package com.neuedu.service.impl;

import com.neuedu.dao.AddXiaoquNumberDao;
import com.neuedu.dao.XiaoQuDao;
import com.neuedu.dao.impl.AddXiaoquNumberDaoImpl;
import com.neuedu.dao.impl.XiaoQuDaoImpl;
import com.neuedu.model.AddXiaoquNumber;
import com.neuedu.model.XiaoQu;
import com.neuedu.service.XiaoQuService;
import com.neuedu.vo.PageVO;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


public class XiaoQuServiceImpl implements XiaoQuService {

    private XiaoQuDao xiaoQuDao=new XiaoQuDaoImpl();

    private AddXiaoquNumberDao addXiaoquNumberDao=new AddXiaoquNumberDaoImpl();

    @Override
    public PageVO<XiaoQu> selectPage(String minDate, String maxDate, String likeName, Integer pageNo, Integer pageSize) {

        Integer limitParams1=(pageNo-1)*pageSize;

        Integer limitParams2=pageSize;
        List<XiaoQu> xiaoQus = xiaoQuDao.selectPage(minDate, maxDate, likeName, limitParams1, limitParams2);
        PageVO<XiaoQu> pageVO=new PageVO<>();
        pageVO.setPageNo(pageNo);
        pageVO.setPageSize(pageSize);
        pageVO.setData(xiaoQus);
        //还差  pageCount页的总数  itemCount数据的总数
        Integer itemCount=xiaoQuDao.count(minDate, maxDate, likeName);
        Integer pageCount=((itemCount%pageSize)==0)?(itemCount/pageSize):(itemCount/pageSize+1);
        pageVO.setItemCount(itemCount);
        pageVO.setPageCount(pageCount);
        return pageVO;
    }

    @Override
    public int addXiaoQu(XiaoQu xiaoQu) {
        //2024-09-10
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);


        AddXiaoquNumber addXiaoquNumber = addXiaoquNumberDao.select();
        String number="XY";
        number=number+format.replace("-","");
        if(addXiaoquNumber.getNumberTime().equals(format)){
            if(addXiaoquNumber.getNumber()<10){
                number=number+"00"+addXiaoquNumber.getNumber();
            }else if(addXiaoquNumber.getNumber()<100){
                number=number+"0"+addXiaoquNumber.getNumber();
            }else{
                number=number+addXiaoquNumber.getNumber();
            }
            addXiaoquNumber.setNumber(addXiaoquNumber.getNumber()+1);
            addXiaoquNumberDao.update(addXiaoquNumber);
        }else{
            addXiaoquNumber.setNumberTime(format);
            addXiaoquNumber.setNumber(1);
            addXiaoquNumberDao.update(addXiaoquNumber);
            number=number+"001";
        }
        xiaoQu.setBianhao(number);
        //处理创建日期
        sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        format = sdf.format(date);
        xiaoQu.setChuangjianshijian(format);
        return xiaoQuDao.insertXiaoQu(xiaoQu);
    }

    @Override
    public int delXiaoQu(Integer[] ids) {
        return xiaoQuDao.deleteXiaoQu(ids);
    }

    @Override
    public XiaoQu queryXiaoQu(Integer id) {
        return xiaoQuDao.selectXiaoQuById(id);
    }

    @Override
    public int saveXiaoQu(XiaoQu xiaoQu) {
        return xiaoQuDao.updateXiaoQu(xiaoQu);
    }

    @Override
    public List<XiaoQu> showAll() {
        List<XiaoQu> xiaoQus = xiaoQuDao.selectAll();
        for (int i = 0; i < xiaoQus.size(); i++) {
            xiaoQus.get(i);
        }
        return xiaoQus;
    }
}
